package com.scau.clothes.api.stock;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.scau.clothes.core.core.entity.AjaxResult;
import com.scau.clothes.stock.model.dto.ClothesNormDto;
import com.scau.clothes.stock.model.entity.ClothesNorm;
import com.scau.clothes.stock.service.ClothesNormService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/clothesNorm")
@RestController
public class ClothesNormController {
    @Resource
    private ClothesNormService clothesNormService;

    @PostMapping("/listClothesNorm")
    @ApiOperation("查询库存")
    public AjaxResult listClothesNorm(@RequestBody ClothesNormDto clothesNormDto){
        List<ClothesNorm> list = clothesNormService.listClothesNorm(clothesNormDto);
        return AjaxResult.success("查询成功",list);
    }

    @PostMapping("/insertClothesNorm")
    @ApiOperation("新增库存")
    public AjaxResult insertClothesNorm(@RequestBody ClothesNormDto clothesNormDto){
        int rows = clothesNormService.insertClothesNorm(clothesNormDto);
        return rows > 0 ? AjaxResult.success("添加库存成功") : AjaxResult.error("添加库存失败");
    }

    @PostMapping("/updateClothesNorm")
    @ApiOperation("更新库存信息")
    public AjaxResult updateClothesNorm(@RequestBody ClothesNorm clothesNorm){
        int rows = clothesNormService.updateClothesNorm(clothesNorm);
        return rows > 0 ? AjaxResult.success("更新库存信息成功") : AjaxResult.error("更新库存信息失败");
    }

    @PostMapping("/deleteClothesNorm")
    @ApiOperation("删除库存")
    public AjaxResult deleteClothesNorm(@RequestBody ClothesNorm clothesNorm){
        int rows = clothesNormService.deleteClothesNorm(clothesNorm);
        return rows > 0 ? AjaxResult.success("删除库存成功") : AjaxResult.error("删除库存失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页操作")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, ClothesNormDto clothesNormDto){
        List<ClothesNorm> list = clothesNormService.listClothesNorm(clothesNormDto);
        Page page = new Page(pageNum, pageSize);
        int total = list.size();
        page.setTotal(total);
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize,total);
        page.addAll(list.subList(startIndex,endIndex));
        PageInfo pageInfo = new PageInfo<>(page);
        return AjaxResult.success(pageInfo);
    }
}
